package com.jsyxdl.conntroller;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.hustcad.plm.caddocapi.service.TyplmCADRelObjectService;
import com.hustcad.plm.pdm.partbom.vo.PartDetailsVO;
import com.hustcad.plm.pdm.user.service.ErpBomService;
import com.hustcad.plm.pdm.user.service.SettingService;
import com.ty.basic.response.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/rest/v1/setting")
public class DocSettingController {

    @Autowired
    SettingService settingService;
    @Autowired
    ErpBomService erpBomService;
    @Autowired
    TyplmCADRelObjectService typlmCADRelObjectService;

    @RequestMapping("/docFile")
    public ResponseResult<PageInfo<Map<String,Object>>> docFile(@RequestBody Map<String,Object> map) {
        PageMethod.startPage((Integer) map.get("pageNum"), (Integer) map.get("pageSize"), true);
        List<Map<String,Object>> list = erpBomService.getAllDoc(map);
        for(int i=0;i<list.size();i++){
            if(list.get(i).get("oid")!=null){
                list.get(i).put("oid",((BigDecimal)list.get(i).get("oid")).toString());
            }
            list.get(i).put("cadid",((BigDecimal)list.get(i).get("cadid")).toString());

            if(list.get(i).get("aoid")!=null){
                list.get(i).put("aoid",((BigDecimal)list.get(i).get("aoid")).toString());
            }
            if(list.get(i).get("boid")!=null){
                list.get(i).put("boid",((BigDecimal)list.get(i).get("boid")).toString());
            }
            String fileName= (String) list.get(i).get("filename");
            if(fileName!=null){
                String update=fileName.substring(0,fileName.lastIndexOf("."));
                String updateFileSuffix=fileName.substring(fileName.lastIndexOf("."));
                list.get(i).put("update",update);
                list.get(i).put("updateFileSuffix",updateFileSuffix);
                String cadName= (String) list.get(i).get("cadname");
                String updateCadNameSuffix=cadName.substring(cadName.lastIndexOf("."));
                list.get(i).put("updateCadNameSuffix",updateCadNameSuffix);
            }


            if(list.get(i).get("partnumber")!=null){
                List<PartDetailsVO> partList=this.typlmCADRelObjectService.queryCadRefPartInfo(new BigInteger(String.valueOf(list.get(i).get("aoid"))));
                if(partList.size()>0){
                    PartDetailsVO partDetailsVO=partList.get(0);
                    Map<String, List<String>> attr=partDetailsVO.getIbaAttributeMap();
                    if(attr.containsKey("规格型号")){
                        List<String> attrList=attr.get("规格型号");
                        if(attrList.size()>0){
                            list.get(i).put("specification",attrList.get(0));
                        }
                    }
                }
            }

        }
        PageInfo<Map<String,Object>> result = new PageInfo(list);
        return ResponseResult.success(result);
    }
    @RequestMapping("/updateDocFile")
    public ResponseResult updateDocFile(@RequestBody Map<String,Object> map) {
        JSONObject obj=JSONObject.parseObject((String) map.get("json"));
        obj.put("cadid",obj.getBigInteger("cadid"));
        erpBomService.updateDocFile(obj.getInnerMap());
        return ResponseResult.success();
    }
}
